<?php
/**
 * 基础物料库存冻结类
 * @author xiayuanjun@shopex.cn
 * @version 1.0
 */
class material_basic_material_stock_freeze{

    //对象类型字段的常量定义
    //订单类型
    const __ORDER = 1;

    //仓库类型
    const __BRANCH = 2;

    //配额ID字段的常量定义
    //非配额即共享库存
    const __SHARE_STORE = -1;

    //门店确认库存
    const __STORE_CONFIRM = -2;

     function __construct(){
        $this->_stockFreezeObj = app::get('material')->model('basic_material_stock_freeze');
     }

    /**
     * 增加基础物料冻结
     * 
     * @param Int $bm_id 基础物料ID
     * @param Int $obj_type 1 订单预占 2 仓库预占
     * @param Int $obj_id 关联对象ID
     * @param String $shop_id 店铺ID
     * @param String $branch_id 仓库ID
     * @param Int $bmsq_id 配额ID  -1代表非配额货品 -2代表门店确认库存的货品 -3代表门店非确认库存的货品
     * @param Int $num 预占数
     * @return Boolean
     */
    public function freeze($bm_id, $obj_type, $obj_id, $shop_id, $branch_id, $bmsq_id, $num){

        if(empty($bm_id) || empty($obj_type) || empty($bmsq_id)){
            return false;
        }

        $num = intval($num);

        switch($obj_type){
            //订单预占
            case 1:
                $filter = array('bm_id'=>$bm_id, 'obj_type'=>1, 'obj_id'=>$obj_id, 'bmsq_id'=>$bmsq_id);
                $insertExtData = array('shop_id'=>$shop_id);
                break;
            //电商仓/门店仓预占
            case 2:
                $filter = array('bm_id'=>$bm_id, 'obj_type'=>2, 'obj_id'=>$obj_id, 'bmsq_id'=>$bmsq_id);
                $insertExtData = array('shop_id'=>$shop_id, 'branch_id'=>$branch_id);
                break;
        }

        //如果是门店确定性预占，总计字段也做更新
        if($obj_type == 2 && $bmsq_id == -2){
            $branchPrdLib        = kernel::single('o2o_branch_product');
            $res = $branchPrdLib->changeStoreConfirmFreeze($branch_id,$bm_id,$num,'+');
            if(!$res){
                return false;
            }
        }

        $freezeRow = $this->_stockFreezeObj->getList('bmsf_id', $filter, 0, 1);
        if($freezeRow){
            $sql = "UPDATE sdb_material_basic_material_stock_freeze SET num=num+".$num." WHERE bmsf_id=".$freezeRow[0]['bmsf_id'];
            return $this->_stockFreezeObj->db->exec($sql);
        }else{
            $insertData = $filter;
            $insertData['num'] = $num;
            if($insertExtData){
                $insertData = array_merge($insertData, $insertExtData);
            }

            return $this->_stockFreezeObj->insert($insertData);
        }
    }


    /**
     * 释放基础物料冻结
     * 
     * @param Int $bm_id 基础物料ID
     * @param Int $obj_type 1 订单预占 2 仓库预占
     * @param Int $obj_id 关联对象ID
     * @param String $branch_id 仓库ID
     * @param Int $bmsq_id 配额ID  -1代表非配额货品 -2代表门店确认库存的货品 -3代表门店非确认库存的货品
     * @param Int $num 预占数
     * @return Boolean
     */
    public function unfreeze($bm_id, $obj_type, $obj_id, $branch_id,$bmsq_id, $num){

        if(empty($bm_id) || empty($obj_type) || empty($bmsq_id)){
            return false;
        }

        $num = intval($num);

        switch($obj_type){
            case 1:
            case 2:
                $sql_where = "WHERE  bm_id =".$bm_id." and obj_type =".$obj_type." and obj_id =".$obj_id." and bmsq_id =".$bmsq_id;
                break;
            default:
                return false;
                break;
        }

        //如果是门店确定性预占，总计字段也做更新
        if($obj_type == 2 && $bmsq_id == -2){
            $branchPrdLib        = kernel::single('o2o_branch_product');
            $res = $branchPrdLib->changeStoreConfirmFreeze($branch_id,$bm_id,$num,'-');
            if(!$res){
                return false;
            }
        }

        $sql = "UPDATE sdb_material_basic_material_stock_freeze SET num=IF((CAST(num AS SIGNED)-$num)>0,num-$num,0) ".$sql_where;
        return $this->_stockFreezeObj->db->exec($sql);
    }

    /**
     * 订单全部发货后，调用该方法删除订单预占记录
     * 
     * @param Int $order_id 订单ID
     * @return Boolean
     */
    public function delOrderFreeze($order_id){

        if(empty($order_id)){
            return false;
        }

        $filter = array(
            'obj_id' => $order_id,
            'obj_type' => 1,
        );
        return $this->_stockFreezeObj->delete($filter);
    }

    /**
     * 发货单发货后，调用该方法删除仓库预占记录
     * 
     * @param Int $delivery_id 发货单ID
     * @return Boolean
     */
    public function delDeliveryFreeze($delivery_id){

        if(empty($delivery_id)){
            return false;
        }

        $filter = array(
            'obj_id' => $delivery_id,
            'obj_type' => 2,
        );
        return $this->_stockFreezeObj->delete($filter);
    }

    /**
     * 根据订单号查询是否有该订单的预占
     * 
     * @param Int $order_id 订单ID
     * @return Boolean
     */
    public function hasOrderFreeze($order_id){

        if(empty($order_id)){
            return false;
        }

        $result = $this->_stockFreezeObj->getList('bmsf_id', array('obj_type'=>1,'obj_id'=>$order_id,'num|than'=>0), 0, 1);
        if($result){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 根据订单号查询是否有该订单的预占
     * 
     * @param Int $bm_id 基础物料ID
     * @param Int $shop_id 店铺ID
     * @return number
     */
    public function getShopFreeze($bm_id, $shop_id){

        if(empty($bm_id) || empty($shop_id)){
            return false;
        }

        $result = $this->_stockFreezeObj->db->selectrow("SELECT sum(num) as total FROM sdb_material_basic_material_stock_freeze WHERE bm_id=".$bm_id." AND obj_type=1 AND shop_id='".$shop_id."'");
        if($result){
            return $result['total'];
        }else{
            return 0;
        }
    }
}
